package cn.xo68.ide.plugin.common.ui.generate;

import com.wy2.generator.database.entity.DatabaseColumn;
import com.wy2.generator.database.entity.DatabaseTable;
import org.jdesktop.swingx.treetable.AbstractTreeTableModel;

/**
 * 代码生成树表模型
 * @author wuxie
 * @date 2018-11-25
 */
public class CodeGenerateModel extends AbstractTreeTableModel {

    public CodeGenerateModel(CodeGenerateNode root) {
        super(root);
    }

    @Override
    public int getColumnCount() {
        return 3;
    }

    @Override
    public Class<?> getColumnClass(int column) {
        return Object.class;
    }

    @Override
    public String getColumnName(int column) {
        switch(column) {
            case 0:
                return "表名/字段名";
            case 1:
                return "实体/字段";
            case 2:
                return "模块";
        }
        return "";
    }

    @Override
    public Object getValueAt(Object o, int i) {
        if(o instanceof CodeGenerateNode && ((CodeGenerateNode) o).getUserObject() instanceof  DatabaseTable){
            DatabaseTable databaseTable = (DatabaseTable) ((CodeGenerateNode) o).getUserObject();
            switch(i) {
                case 0:
                    return databaseTable.getSqlName();
                case 1:
                    return databaseTable.getEntityName();
                case 2:
                    return databaseTable.getModule();
            }
        }else if(o instanceof DatabaseColumn){
            DatabaseColumn databaseColumn = (DatabaseColumn) ((CodeGenerateNode) o).getUserObject();
            switch(i) {
                case 0:
                    return databaseColumn.getSqlName();
                case 1:
                    return databaseColumn.getJavaProperty();
                case 2:
                    return databaseColumn.getFullJavaType();
            }
        }
        return null;

    }

    @Override
    public Object getChild(Object parent, int index) {
        if(parent instanceof CodeGenerateNode && ( ((CodeGenerateNode) parent).getUserObject() == null
                || ((CodeGenerateNode) parent).getUserObject() instanceof DatabaseTable )){
            return ((CodeGenerateNode) parent).getChildAt(index);
        }
        return null;
    }

    @Override
    public int getChildCount(Object parent) {
        if(parent instanceof CodeGenerateNode && ( ((CodeGenerateNode) parent).getUserObject() == null
                || ((CodeGenerateNode) parent).getUserObject() instanceof DatabaseTable )){
            return ((CodeGenerateNode) parent).getChildCount();
        }
        return 0;
    }

    @Override
    public int getIndexOfChild(Object parent, Object child) {
        if(parent instanceof CodeGenerateNode && ( ((CodeGenerateNode) parent).getUserObject() == null
                || ((CodeGenerateNode) parent).getUserObject() instanceof DatabaseTable )){
            for(int i=0; i < ((CodeGenerateNode) parent).getChildCount(); i++){
                if(child.equals(((CodeGenerateNode) parent).getChildAt(i))){
                    return i;
                }
            }
        }
        return -1;
    }
}
